<template>
	<div class="dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :close-on-click-modal="false" width="500px">
			<el-form ref="formRef" :model="state.ruleForm" :rules="state.rules" :disabled="state.dialog.type === 'view' ? true : false">
				<el-form-item label="针对商品" prop="status">
					<el-select v-model="state.ruleForm.status">
						<el-option label="全部商品" :value="0" />
						<el-option label="上架商品" :value="1" />
						<el-option label="下架商品" :value="2" />
					</el-select>
				</el-form-item>
				<el-form-item label="改价类型" prop="type">
					<el-radio-group v-model="state.ruleForm.type">
						<el-radio :value="1">增加</el-radio>
						<el-radio :value="2">减少</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="汇率数值">
					<el-input
						v-model="state.ruleForm.proportion"
						@input="onVerifyNumberIntegerAndFloatProportion"
						placeholder="请输入汇率数值"
					>
						<template #append>‰</template>
					</el-input>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button @click="onCancel" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.dialog.submitLoading" size="default">提交执行任务</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { nextTick, reactive, ref } from 'vue';

import { goodsApi } from '/@/api';
import { verifyNumberIntegerAndFloat4 } from '/@/utils/toolsValidate';
import { floatDivide } from '/@/utils/dictionary';
import { ElMessage } from 'element-plus';

let callback: Function = () => {};
const GoodsApi = goodsApi()
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '添加改价任务',
		submitLoading: false,
	},
	ruleForm: {
		status: 0,
		type: 1,
		proportion: '',
	},
	rules: {
		proportion: [{ required: true, message: '汇率数值不能为空', trigger: ['blur'] }],
	},
});
const formRef = ref();

/********** 触发事件 **********/
// 汇率数值过滤
const onVerifyNumberIntegerAndFloatProportion = (val: string) => {
	state.ruleForm.proportion = verifyNumberIntegerAndFloat4(val);
};
// 取消
const onCancel = () => {
	state.dialog = {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	};
};
// 提交
const onSubmit = () => {
	formRef.value.validate((valid: boolean) => {
		if (!valid) return;
		state.dialog.submitLoading = true
			_goodsChangePriceAdd({
				...state.ruleForm,
				proportion: floatDivide(state.ruleForm.proportion, 1000),
			});
	});
};

/********** 方法事件 **********/
// 打开
const openDialog = (fn: Function) => {
	state.dialog.isShowDialog = true;
	callback = fn;
	nextTick(() => {
		formRef.value.resetFields();
	})
};

/********** 网络请求 **********/
// 添加商品改价
const _goodsChangePriceAdd = (params: Object) => {
	GoodsApi.goodsChangePriceAdd(params)
		.then(() => {
			ElMessage.success('任务创建成功，请耐心等待任务执行完成');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};

/********** 方法向父组件暴露 **********/
defineExpose({ openDialog });
</script>
